package com.prawn.ift.datasearch.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.prawn.ift.datasearch.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.ift.datasearch.dao.CheckItemDao;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class CheckItemService {

	@Autowired
	private CheckItemDao checkItemDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<CheckItem> findAll() {
		return checkItemDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<CheckItem> findSearch(Map whereMap, int page, int size) {
		Specification<CheckItem> specification = createSpecification(whereMap);
		Sort sort=new Sort(Sort.Direction.ASC,"channelNo");
		PageRequest pageRequest =  PageRequest.of(page-1, size,sort);
		return checkItemDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<CheckItem> findSearch(Map whereMap) {
		Specification<CheckItem> specification = createSpecification(whereMap);
		return checkItemDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "checkItem",key = "#id")
	public CheckItem findById(String id) {
		return checkItemDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param checkItem
	 */
	public void add(CheckItem checkItem) {
		checkItem.setId( idWorker.nextId()+"" );
		checkItemDao.save(checkItem);
	}

	/**
	 * 修改
	 * @param checkItem
	 */
	@CacheEvict(value = "checkItem",key = "#checkItem.id")
	public void update(CheckItem checkItem) {

		checkItemDao.save(checkItem);
	}

	/**
	 * 删除
	 * @param id
	 */
	@CacheEvict(value = "checkItem",key = "#id")
	public void deleteById(String id) {
		checkItemDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<CheckItem> createSpecification(Map searchMap) {

		return new Specification<CheckItem>() {

			@Override
			public Predicate toPredicate(Root<CheckItem> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 检测代码
                if (searchMap.get("code")!=null && !"".equals(searchMap.get("code"))) {
                	predicateList.add(cb.like(root.get("code").as(String.class), "%"+(String)searchMap.get("code")+"%"));
                }
                // 名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 设备型号
                if (searchMap.get("devModel")!=null && !"".equals(searchMap.get("devModel"))) {
                	predicateList.add(cb.like(root.get("devModel").as(String.class), "%"+(String)searchMap.get("devModel")+"%"));
                }
                // 校准公式
                if (searchMap.get("chCorrFormula")!=null && !"".equals(searchMap.get("chCorrFormula"))) {
                	predicateList.add(cb.like(root.get("chCorrFormula").as(String.class), "%"+(String)searchMap.get("chCorrFormula")+"%"));
                }
                // 校准周期
                if (searchMap.get("chCorrCyc")!=null && !"".equals(searchMap.get("chCorrCyc"))) {
                	predicateList.add(cb.like(root.get("chCorrCyc").as(String.class), "%"+(String)searchMap.get("chCorrCyc")+"%"));
                }
                // 量程下限
                if (searchMap.get("chMin")!=null && !"".equals(searchMap.get("chMin"))) {
                	predicateList.add(cb.like(root.get("chMin").as(String.class), "%"+(String)searchMap.get("chMin")+"%"));
                }
                // 量程上限
                if (searchMap.get("chMax")!=null && !"".equals(searchMap.get("chMax"))) {
                	predicateList.add(cb.like(root.get("chMax").as(String.class), "%"+(String)searchMap.get("chMax")+"%"));
                }
                // 小数位数
                if (searchMap.get("chDecDig")!=null && !"".equals(searchMap.get("chDecDig"))) {
                	predicateList.add(cb.like(root.get("chDecDig").as(String.class), "%"+(String)searchMap.get("chDecDig")+"%"));
                }
                // 类型编号
                if (searchMap.get("chtypeNo")!=null && !"".equals(searchMap.get("chtypeNo"))) {
                	predicateList.add(cb.like(root.get("chtypeNo").as(String.class), "%"+(String)searchMap.get("chtypeNo")+"%"));
                }
                // 采集量显示的图片         预留字段
                if (searchMap.get("chFile")!=null && !"".equals(searchMap.get("chFile"))) {
                	predicateList.add(cb.like(root.get("chFile").as(String.class), "%"+(String)searchMap.get("chFile")+"%"));
                }
                // 单位
                if (searchMap.get("unit")!=null && !"".equals(searchMap.get("unit"))) {
                	predicateList.add(cb.like(root.get("unit").as(String.class), "%"+(String)searchMap.get("unit")+"%"));
                }
                // 校正后单位        预留
                if (searchMap.get("corrUnit")!=null && !"".equals(searchMap.get("corrUnit"))) {
                	predicateList.add(cb.like(root.get("corrUnit").as(String.class), "%"+(String)searchMap.get("corrUnit")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 删除标记
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
